package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.ExchangeAmountMoneyDivideDetail;
import cc.yugu.yhdw.common.domain.hdw.ExchangeOrderMoneyDivide;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.basic.AgentMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.AgencyMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.ExchangeOrderMoneyDivideMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.SupplierMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * 换电订单金额分配业务
 *
 * @Date:Created in 2018/7/9 10:17
 * @Author:
 * @Modified By: zjn 2018/7/9 10:17
 */ 
@Service
public class ExchangeOrderMoneyDivideService extends AbstractService {
    @Autowired
    ExchangeOrderMoneyDivideMapper exchangeOrderMoneyDivideMapper;
    @Autowired
    AgencyMapper agencyMapper;
    @Autowired
    AgentMapper agentMapper;
    @Autowired
    SupplierMapper supplierMapper;
    /**
    * 分页查询
    *
    * @Param: [exchangeOrderMoneyDivide]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/9  10:18
    */
    public Page findPage(ExchangeOrderMoneyDivide exchangeOrderMoneyDivide) {
        Page page = exchangeOrderMoneyDivide.buildPage();
        // 查询换电订单的金额分配信息是否存在
        page.setTotalItems(exchangeOrderMoneyDivideMapper.findPageCount(exchangeOrderMoneyDivide));
        exchangeOrderMoneyDivide.setBeginIndex(page.getOffset());
        // 查询换电订单的金额分配信息
        List<ExchangeOrderMoneyDivide> list = exchangeOrderMoneyDivideMapper.findPageResult(exchangeOrderMoneyDivide);
        ExchangeAmountMoneyDivideDetail.OrgType[] types = ExchangeAmountMoneyDivideDetail.OrgType.values();
        // 遍历
        for (ExchangeOrderMoneyDivide e : list) {
                for (int i = 0; i < types.length; i++) {
                    if (e.getOrgType() == types[i].getValue()) {
                        // 获取分配单位类型名称
                        e.setOrgTypeName(types[i].getName());
                    }
                }
                // 如果分配单位类型
                if (e.getOrgType() == ExchangeAmountMoneyDivideDetail.OrgType.HQ.getValue()) {
                    e.setOrgIdName("总部");
                } else if (e.getOrgType() == ExchangeAmountMoneyDivideDetail.OrgType.AGENT.getValue()) {
                    // 设置分配单位名称为运营商名称
                    e.setOrgIdName(agentMapper.find(e.getOrgId()).getAgentName());
                } else if (e.getOrgType() == ExchangeAmountMoneyDivideDetail.OrgType.AGENCY.getValue()) {
                    e.setOrgIdName(agencyMapper.find(e.getOrgId()).getAgencyName());
                } else {
                    e.setOrgIdName(supplierMapper.find(e.getOrgId()).getSupplierName());
                }
            }
        page.setResult(list);
        return page;
    }
}
